package com.example.demo.utils;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.dto.UserDTO;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

public class JwtUtils {
    //7天过期
    private static long expire = 604800;
    // 32位秘钥
    private static String secret = "abcdfghiabcdfghiabcdfghiabcdfghi";
    //生成token
    public static String generateToken(UserDTO userDTO){
        Date now = new Date();
        Date expiration = new Date( now . getTime() + 1000 * expire);
        return Jwts.builder( )
                .setHeaderParam("type","JwT")
                .setSubject(JSONObject.toJSONString(userDTO))
                .setIssuedAt(now)
                .setExpiration(expiration)
                .signWith(SignatureAlgorithm.HS512,secret)
                .compact();
    }

    //解析token
    public static Claims getClaimsByToken(String token){
        return Jwts.parser()
                .setSigningKey(secret)
                .parseClaimsJws(token)
                .getBody();
    }

}
